package com.qf.service.impl;

import com.qf.dao.OrderDao;
import com.qf.dao.impl.OrderDaoImpl;
import com.qf.entity.Cart;
import com.qf.entity.Order;
import com.qf.entity.OrderDetail;
import com.qf.service.CartService;
import com.qf.service.OrderDetailService;
import com.qf.service.OrderService;
import com.qf.utils.DruidUtils;

import java.util.List;

/**
 * @author 10202
 * @version V1.0
 * @Project xiaomi
 * @Package com.qf.service.impl
 * @Date 2023/4/14 22:14
 */
public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao=new OrderDaoImpl();
    @Override
    public void addOrder(Order order, List<Cart> cartList) {

        try {
            DruidUtils.begin();
            //1 添加订单表
            orderDao.addOrder(order);

            //2.添加订单详细表
            OrderDetailService orderDetailService =new OrderDetailServiceImpl();

            for (Cart cart : cartList) {
                OrderDetail orderDetail=new OrderDetail(null,order.getId(),cart.getPid(),cart.getNum(),cart.getMoney());
                orderDetailService.add(orderDetail);
            }

            //3.删除购物车信息
            CartService cartService=new CartServiceImpl();
            cartService.deleteCartByUid(order.getUid());
            DruidUtils.commit();
        } catch (Exception e) {
            DruidUtils.rollback();
            throw new RuntimeException(e);
        }finally {
            DruidUtils.close();
        }


    }

    @Override
    public List<Order> getOrderByUid(Integer id) {

        return orderDao.getOrderByUid(id);
    }

    @Override
    public Order queryOrderByOid(String oid) {
        return orderDao.queryOrderByOid(oid);
    }

    @Override
    public void updateOrderByOrder(Order order) {
        orderDao.updateOrderByOrder(order);
    }
}
